package com.chuqiyun.authorization.controller.user;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chuqiyun.authorization.annotation.ApiCheck;
import com.chuqiyun.authorization.entity.Commodity;
import com.chuqiyun.authorization.entity.Shoppingcart;
import com.chuqiyun.authorization.entity.User;
import com.chuqiyun.authorization.service.CommodityService;
import com.chuqiyun.authorization.service.ShoppingcartService;
import com.chuqiyun.authorization.service.UserService;
import com.chuqiyun.authorization.utils.ResponseResult;
import com.chuqiyun.authorization.utils.ServletUtil;
import com.chuqiyun.authorization.utils.exception.UnauthorizedException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * @author mryunqi
 * @date 2023/4/28
 */
@RestController
public class UserShoppingCartController {
    @Resource
    private UserService userService;
    @Resource
    private CommodityService commodityService;
    @Resource
    private ShoppingcartService shoppingcartService;
    @Value("${config.secret}")
    private String secret;

    /**
    * @Author: mryunqi
    * @Description: 添加商品到购物车
    * @DateTime: 2023/4/28 18:27
    */
    @ApiCheck
    @PostMapping("/user/insertShoppingCart")
    public ResponseResult<String> insertShoppingCart(HttpServletRequest request,
                                                     @RequestBody Shoppingcart shoppingcart) throws UnauthorizedException {
        Commodity commodityInfo = commodityService.getById(shoppingcart.getCommodityId());
        User cookieUser = ServletUtil.getLoginMember(request,secret);
        if (Objects.isNull(commodityInfo)){
            return ResponseResult.fail("该商品不存在");
        }
        assert cookieUser != null;
        User user = userService.getUser(cookieUser.getPhone());
        shoppingcart.setUserUuid(user.getUuid());
        if (shoppingcartService.save(shoppingcart)){
            return ResponseResult.ok("添加成功");
        }
        return ResponseResult.fail("添加失败");
    }

    /**
    * @Author: mryunqi
    * @Description: 获取购物车列表
    * @DateTime: 2023/4/28 18:44
    */
    @ApiCheck
    @GetMapping("/user/getShoppingCartList")
    public ResponseResult<Page<Shoppingcart>> getShoppingCartList(HttpServletRequest request,
                                                                  @RequestParam(value = "page") int page,
                                                                  @RequestParam(value = "size") int size) throws UnauthorizedException{
        User cookieUser = ServletUtil.getLoginMember(request,secret);
        assert cookieUser != null;
        User user = userService.getUser(cookieUser.getPhone());
        QueryWrapper<Shoppingcart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_uuid",user.getUuid());
        return ResponseResult.ok(shoppingcartService.getShoppingCartList(page,size,queryWrapper));
    }

}
